package jp.joogoo.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import jp.joogoo.web.model.M_joogoo;
import jp.joogoo.web.service.AuthorizeService;
import jp.joogoo.web.service.ContentsService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class FetcherController extends Controller {

    private static final ContentsService service = new ContentsService();
    private static final AuthorizeService auth = new AuthorizeService();

    private static final Logger logger = Logger.getLogger(FetcherController.class.getName());

    @Override
	protected Navigation run() throws Exception {
        String userId = asString("userId");
        if (userId == null || userId.length() == 0) {
            logger.severe("invalid access, no user id is specified.");
        }
        List<M_joogoo> accounts = auth.getAccounts(userId);
        int success = 0;
        for (M_joogoo account : accounts) {
            try {
                service.fetch(account);
                success++;
            } catch (IOException e) {
                logger.severe(account.getKey() + ": " + e.getMessage());
            }
        }
        logger.info("Total=" + accounts.size() + ", success=" + success);
        return null;
	}
}
